use super::*;

mod delta_set;
mod item_store;

pub use delta_set::*;
pub use item_store::*;

/// A variation coordinate in a normalized coordinate system.
///
/// Basically any number in a -1.0..1.0 range.
/// Where 0 is a default value.
///
/// The number is stored as f2.16
#[repr(transparent)]
#[derive(Clone, Copy, PartialEq, Eq, Default, Debug)]
pub struct NormalizedCoordinate(pub i16);

impl From<i16> for NormalizedCoordinate {
	/// Creates a new coordinate.
	///
	/// The provided number will be clamped to the -16384..16384 range.
	#[inline]
	fn from(n: i16) -> Self {
		NormalizedCoordinate(Self::i16_bound(-16384, n, 16384))
	}
}

impl From<f32> for NormalizedCoordinate {
	/// Creates a new coordinate.
	///
	/// The provided number will be clamped to the -1.0..1.0 range.
	#[inline]
	fn from(n: f32) -> Self {
		NormalizedCoordinate((Self::f32_bound(-1.0, n, 1.0) * 16384.0) as i16)
	}
}

impl NormalizedCoordinate {
	#[inline]
	pub fn i16_bound(min: i16, val: i16, max: i16) -> i16 {
		use core::cmp;
		cmp::max(min, cmp::min(max, val))
	}
	#[inline]
	pub fn f32_bound(min: f32, val: f32, max: f32) -> f32 {
		debug_assert!(min.is_finite());
		debug_assert!(val.is_finite());
		debug_assert!(max.is_finite());

		if val > max {
			return max;
		} else if val < min {
			return min;
		}

		val
	}
	/// Returns the coordinate value as f2.14.
	#[inline]
	pub fn get(self) -> i16 {
		self.0
	}
}

/// A font variation value.
///
/// # Example
///
/// ```
/// use ttf_parser::{Variation, Tag};
///
/// Variation { axis: Tag::from_bytes(b"wght"), value: 500.0 };
/// ```
#[derive(Clone, Copy, PartialEq, Debug)]
pub struct Variation {
	/// An axis tag name.
	pub axis: Tag,
	/// An axis value.
	pub value: f32,
}
